package com.wgts.wgts.mvp.view.activity.classification;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.netmodle.resp.AllClassTypeEntity;
import com.wgts.wgts.netmodle.resp.ClassificationEntity;

import java.util.List;

public class ClassificationAdapter extends RecyclerView.Adapter<ClassificationAdapter.ClassificationViewHolder> {


    private List<AllClassTypeEntity> classificationEntities;

    private Context context;

    private OnItemClickListener onItemClickListener;

    public ClassificationAdapter(List<AllClassTypeEntity> classificationEntities, Context context){
        this.classificationEntities = classificationEntities;
        this.context = context;
    }


    public interface OnItemClickListener {
        public void onItemClick(View view, int postion);
    }

    public void setOnItemClickView(OnItemClickListener onItemClickListener ){
        this.onItemClickListener = onItemClickListener;
    }


        @NonNull
    @Override
    public ClassificationViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ClassificationViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.classification_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ClassificationViewHolder classificationViewHolder, final int i) {

        final AllClassTypeEntity classificationEntity = classificationEntities.get(i);

        if(classificationEntity.isSelect()){
            classificationViewHolder.viewIndicator.setVisibility(View.VISIBLE);
            classificationViewHolder.tvClassification.setTextColor(ContextCompat.getColor(context, R.color.home_page_color));
            classificationViewHolder.linearRoot.setBackgroundColor(ContextCompat.getColor(context, R.color.whrite));
        }else{
            classificationViewHolder.viewIndicator.setVisibility(View.INVISIBLE);
            classificationViewHolder.tvClassification.setTextColor(ContextCompat.getColor(context, R.color.nomal));
            classificationViewHolder.linearRoot.setBackgroundColor(ContextCompat.getColor(context, R.color.app_bg));
        }

        classificationViewHolder.tvClassification.setText(classificationEntity.typeName);

        classificationViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(classificationViewHolder.itemView, i);
            }
        });

    }

    @Override
    public int getItemCount() {
        return classificationEntities.size();
    }

    public class ClassificationViewHolder extends RecyclerView.ViewHolder{
        public View viewIndicator;
        public TextView tvClassification;
        public View itemView;
        public LinearLayout linearRoot;
        public ClassificationViewHolder(@NonNull View itemView) {
            super(itemView);
            viewIndicator = itemView.findViewById(R.id.view_indicator);
            tvClassification = itemView.findViewById(R.id.tv_classification);
            linearRoot = itemView.findViewById(R.id.linear_root);
            this.itemView = itemView;
        }
    }
}
